package application;

import java.io.IOException;
import java.util.ArrayList;

/**
 * This class takes the input data from a file, parses it and creates an ArrayList 
 * containing ProjectStatistics objects. The variables in the ProjectStatistics objects
 * are assigned to their respective EffortMult enums. The lowest and highest value of all
 * variables of all projects are also stored in two separate lists.
 */
public class DataParser {
	private ArrayList<ProjectStatistics> list;
	private double[] lowest;
	private double[] highest;
	private int count;
	
	public DataParser(ArrayList<ProjectStatistics> list) {
		this.list = list;
		//TODO lowest och highest storlek ska ändras om vi utökar antalet variabler.
		lowest = new double[17]; 
		highest = new double[17]; 
	}
	
	/**
	 * Parses the data from the file and returns an ArrayList containing ProjectStatistics objects.
	 * @param fileName name of file to read data from
	 * @return list an ArrayList containing ProjectStatistics objects
	 * @throws IOException if file was not found
	 */
	public ArrayList<ProjectStatistics> getDataList(String fileName) throws IOException {
		TextReader tr = new TextReader(fileName);
		count = 0;
		while (tr.hasNextLine()) {
			String line = tr.nextLine();
			parseData(line);
			count++;
		}
		return list;
	}
	
	/**
	 * Returns a list containing the lowest values of all variables of all projects.
	 */
	public double[] getLowestValues() {
		return lowest;
	}
	
	/**
	 * Returns a list containing the highest values of all variables of all projects. 
	 */
	public double[] getHighestValues() {
		return highest;
	}
	
	private void parseData(String line) {
		String[] dataList = line.split(",\\s+");
		//TODO Antal variabler ska justeras om vi lägger till fler, samt for-loopens storlek.
		EffortMult[] em = new EffortMult[15];
		double ksloc = Double.parseDouble(dataList[15]);
		double actualEffort = Double.parseDouble(dataList[16]);
		for (int i = 0; i < 17; i++) { 
			if (i < 15) {
				em[i] = getEffortMultipler(dataList[i], i);
			} else {
				addMinMaxKsloc(ksloc, i);
				addMinMaxActualEffort(actualEffort, i);
			}
		}
		ProjectStatistics ps = new ProjectStatistics(em[0], em[1], em[2], em[3], em[4], em[5],
				em[6], em[7], em[8], em[9], em[10], em[11], em[12], em[13], em[14], ksloc, actualEffort);
		list.add(ps);
	}

	private void addMinMaxKsloc(double ksloc, int i) {
		if (count == 0) {
			lowest[15] = ksloc;
			highest[15] = ksloc;
		} else if (lowest[i] > ksloc) {
			lowest[i] = ksloc;
		} else if (highest[i] < ksloc) {
			highest[i] = ksloc;
		}
	}
	
	private void addMinMaxActualEffort(double actualEffort, int i) {
		if (count == 0) {
			lowest[16] = actualEffort;
			highest[16] = actualEffort;
		} else if (lowest[i] > actualEffort) {
			lowest[i] = actualEffort;
		} else if( highest[i] < actualEffort) {
			highest[i] = actualEffort;
		}
	}

	private EffortMult getEffortMultipler(String s, int i) {
		EffortMult em = EffortMult.NOMINAL;
		if (s.equalsIgnoreCase("very_low")) {
			em = EffortMult.VERY_LOW;
		} else if (s.equalsIgnoreCase("low")) {
			em = EffortMult.LOW;
		} else if (s.equalsIgnoreCase("high")) {
			em = EffortMult.HIGH;
		} else if (s.equalsIgnoreCase("very_high")) {
			em = EffortMult.VERY_HIGH;
		} else if (s.equalsIgnoreCase("extra_high")) {
			em = EffortMult.EXTRA_HIGH;
		}
		Variables v = Variables.values()[i];
		if(count == 0) {
			lowest[i] = v.getValue(em);
			highest[i] = v.getValue(em);
		} else {
			if (lowest[i] > v.getValue(em)) {
				lowest[i] = v.getValue(em);
			} else if (highest[i] < v.getValue(em)) {
				highest[i] = v.getValue(em);
			}
		}
		return em;
	}

	//TODO Ska tas bort, används bara för att testa så att inläsningen blir rätt.
	public static void main(String[] args) {
		
	}
	
}
